/*
 * s_common_ble.c
 *
 *  Created on: Sep 27, 2020
 *      Author: hemingway
 */
#include "s_common_ble.h"
#include <stdio.h>
#include <string.h>
#include "h_cc2640r2f_define.h"
#include "simple_peripheral_oad_offchip.h"

#include "h_cc2640r2f_uart.h"

#include "s_common_evt.h"


#define S_COMMON_BLE_MAX_SEND_DATALENGTH                      45
#define S_COMMON_BLE_MAX_SINGLE_SEND_DATALENGTH               20

// Data for sending
static unsigned char s_common_ble_send_bleData_point[S_COMMON_BLE_MAX_SEND_DATALENGTH];
static volatile unsigned short s_common_ble_send_bleData_length = 0U;
static volatile unsigned char s_common_ble_send_bleData_long_data_flag = 0U;
static volatile unsigned short s_common_ble_send_bleData_start_index = 0U;
static volatile unsigned short s_common_ble_send_bleData_left_length = 0U;



void s_common_ble_advertising(void)
{
//    H_CC2640R2F_UART_TxBuff("1234", 4);
    // printf("After advertising\r\n");
//    s_common_test();
}

void s_common_ble_connected(void)
{

}

void s_common_ble_disconnected(void)
{

}

void s_common_ble_send_bleData(unsigned char *data, unsigned short length)
{
    if(length > S_COMMON_BLE_MAX_SEND_DATALENGTH) {
        s_common_ble_send_bleData_length = 0U;
    } else {
        memcpy(s_common_ble_send_bleData_point, data, length);
        s_common_ble_send_bleData_length = length;
    }

    if(s_common_ble_send_bleData_length > S_COMMON_BLE_MAX_SINGLE_SEND_DATALENGTH) {
        s_common_ble_send_bleData_long_data_flag = 1U;
        s_common_ble_send_bleData_start_index = 0U;
        s_common_ble_send_bleData_left_length = s_common_ble_send_bleData_length - S_COMMON_BLE_MAX_SINGLE_SEND_DATALENGTH;
        s_common_ble_send_bleData_length = S_COMMON_BLE_MAX_SINGLE_SEND_DATALENGTH;
    }

    SimplePeripheral_SingleNotify();
}

unsigned char *s_common_ble_send_bleData_get_point(void)
{
    return s_common_ble_send_bleData_point;
}

unsigned short s_common_ble_send_bleData_get_length(void)
{
    return s_common_ble_send_bleData_length;
}

signed char s_common_ble_handle_receivedData(unsigned char *data, unsigned short len)
{
    s_common_evt_enqueue(s_common_ble_evt, data, len, 0);
    return 0;
}

void s_msg_ble_long_sendData_update(void)
{
    s_common_ble_send_bleData_start_index += s_common_ble_send_bleData_length;

    if(s_common_ble_send_bleData_left_length <= S_COMMON_BLE_MAX_SINGLE_SEND_DATALENGTH) {
        s_common_ble_send_bleData_length = s_common_ble_send_bleData_left_length;
        s_common_ble_send_bleData_left_length = 0U;
    } else {
        s_common_ble_send_bleData_left_length -= S_COMMON_BLE_MAX_SINGLE_SEND_DATALENGTH;
        s_common_ble_send_bleData_length = S_COMMON_BLE_MAX_SINGLE_SEND_DATALENGTH;
    }

    SimplePeripheral_SingleNotify();
}

//#include "s_common_loopClock.h"

//#define TEST_TIME           1
//
//unsigned int callback_times = 0;
//
//static void s_common_timer_callback(s_common_loopClock_type type)
//{
//    printf("The type is %d-%d\r\n", type, callback_times++);
//    if(type >= 5) {
//        // del
//        printf("del rslt %d\r\n", s_common_loopClock_specialEvt_del(1));
//        printf("del rslt %d\r\n", s_common_loopClock_specialEvt_del(2));
//        printf("add rslt %d\r\n", s_common_loopClock_specialEvt_add(1, &s_common_timer_callback, TEST_TIME));
//        printf("add rslt %d\r\n", s_common_loopClock_specialEvt_add(2, &s_common_timer_callback, TEST_TIME));
//        printf("start rslt %d\r\n", s_common_loopClock_specialEvt_start(1));
//    } else {
//        // start next
//        printf("start rslt %d\r\n", s_common_loopClock_specialEvt_start(type+1));
//    }
//
//}
//
//void s_common_test(void)
//{
//
//    printf("add rslt %d\r\n", s_common_loopClock_specialEvt_add(1, &s_common_timer_callback, TEST_TIME));
//    printf("add rslt %d\r\n", s_common_loopClock_specialEvt_add(2, &s_common_timer_callback, TEST_TIME));
//    printf("add rslt %d\r\n", s_common_loopClock_specialEvt_add(3, &s_common_timer_callback, TEST_TIME));
//    printf("add rslt %d\r\n", s_common_loopClock_specialEvt_add(4, &s_common_timer_callback, TEST_TIME));
//    printf("add rslt %d\r\n", s_common_loopClock_specialEvt_add(5, &s_common_timer_callback, TEST_TIME));
//
//    printf("start rslt %d\r\n", s_common_loopClock_specialEvt_start(1));
//
//}
